<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="clr-namespace:Black.SurfaceToolkit.Controls"
    xmlns:converters="clr-namespace:Black.SurfaceToolkit.Converters"
    xmlns:core="clr-namespace:System;assembly=mscorlib">
    
    <Style TargetType="{x:Type controls:CoverFlowItem}">
        <Style.Resources>
            <converters:AffineFunctionConverter x:Key="ScaleAffineConverter"
                                                X1="0.0" Y1="1.0" X2="140.0" Y2="0.5"
                                                P1Behavior="Invert" P2Behavior="Hold" EnsureBehaviors="True" />
            <converters:AffineFunctionConverter x:Key="OpacityAffineConverter"
                                                X1="0.0" Y1="1.0" X2="25.0" Y2="0.9"
                                                P1Behavior="Invert" P2Behavior="Hold" EnsureBehaviors="True" />
            <converters:AffineFunctionConverter x:Key="PositionClipper"
                                                X1="0.0" Y1="0.0" X2="2.0" Y2="2.0"
                                                P1Behavior="Invert" P2Behavior="Hold" EnsureBehaviors="True" />
            <core:Double x:Key="FollowingMarginBase">5.0</core:Double>
            <core:Double x:Key="PrecedingMarginBase">5.0</core:Double>
            <core:Double x:Key="MidFollowingMargin">25.0</core:Double>
            <core:Double x:Key="MidPrecedingMargin">25.0</core:Double>
            <core:Double x:Key="FollowingMarginMaximum">50.0</core:Double>
            <core:Double x:Key="PrecedingMarginMaximum">50.0</core:Double>
            <Duration x:Key="MoveDuration">00:00:00.5</Duration>
            <Duration x:Key="Margin0Duration">00:00:00.5</Duration>
            <Duration x:Key="Margin1Duration">00:00:00.5</Duration>
            <Duration x:Key="Margin2Duration">00:00:00.5</Duration>
        </Style.Resources>
        <Setter Property="FollowingMargin" Value="{StaticResource FollowingMarginBase}" />
        <Setter Property="PrecedingMargin" Value="{StaticResource PrecedingMarginBase}" />
        <Setter Property="ClipToBounds" Value="False" />
        <Setter Property="VerticalPathAlignment" Value="Center" />
        <Setter Property="HorizontalPathAlignment" Value="Center" />
        <Setter Property="FreeSpaceMode" Value="Fixed" />
        <Setter Property="Opacity" Value="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Distance, Converter={StaticResource OpacityAffineConverter}}" />
        <Setter Property="RenderTransform">
            <Setter.Value>
                <ScaleTransform ScaleX="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type controls:CoverFlowItem}}, Path=Distance, Converter={StaticResource ScaleAffineConverter}, FallbackValue=1.0}"
                                ScaleY="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type controls:CoverFlowItem}}, Path=Distance, Converter={StaticResource ScaleAffineConverter}, FallbackValue=1.0}" />
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type controls:CoverFlowItem}">
                    <Border SnapsToDevicePixels="true"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Padding="{TemplateBinding Padding}">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                    </Border>
                    <ControlTemplate.Resources>
                        <Storyboard x:Key="FreeSpaceStoryboard">
                            <DoubleAnimation Storyboard.TargetProperty="PrecedingFreeSpace"
                                             To="0" Duration="{StaticResource MoveDuration}" />
                        </Storyboard>
                        <Storyboard x:Key="PositionStoryboard">
                            <DoubleAnimation Storyboard.TargetProperty="Distance"
                                             To="0.0" Duration="{StaticResource MoveDuration}" />
                        </Storyboard>
                    </ControlTemplate.Resources>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFreeSpaceNeeded" Value="True">
                            <Trigger.EnterActions>
                                <RemoveStoryboard BeginStoryboardName="FreeSpaceStoryboard1" />
                                <RemoveStoryboard BeginStoryboardName="FreeSpaceStoryboard2" />
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <RemoveStoryboard BeginStoryboardName="FreeSpaceStoryboard1" />
                                <BeginStoryboard Name="FreeSpaceStoryboard2" Storyboard="{StaticResource FreeSpaceStoryboard}" HandoffBehavior="Compose" />
                            </Trigger.ExitActions>
                        </Trigger>
                        <Trigger Property="PrecedingFreeSpace" Value="0.0">
                            <Trigger.ExitActions>
                                <RemoveStoryboard BeginStoryboardName="FreeSpaceStoryboard2" />
                                <BeginStoryboard Name="FreeSpaceStoryboard1" Storyboard="{StaticResource FreeSpaceStoryboard}" HandoffBehavior="Compose" />
                            </Trigger.ExitActions>
                        </Trigger>
                        <Trigger Property="IsSelected" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard Name="PositionBeginStoryboard" Storyboard="{StaticResource PositionStoryboard}" />
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <RemoveStoryboard BeginStoryboardName="PositionBeginStoryboard" />
                            </Trigger.ExitActions>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=RelativeIndex, Converter={StaticResource PositionClipper}}" Value="0.0">
                <DataTrigger.EnterActions>
                    <BeginStoryboard x:Name="Margin0BeginStoryboard">
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="FollowingMargin"
                                             To="{StaticResource FollowingMarginMaximum}" Duration="{StaticResource Margin0Duration}" />
                            <DoubleAnimation Storyboard.TargetProperty="PrecedingMargin"
                                             To="{StaticResource PrecedingMarginMaximum}" Duration="{StaticResource Margin0Duration}" />
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>
                <DataTrigger.ExitActions>
                    <RemoveStoryboard BeginStoryboardName="Margin0BeginStoryboard" />
                </DataTrigger.ExitActions>
            </DataTrigger>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=RelativeIndex, Converter={StaticResource PositionClipper}}" Value="1.0">
                <DataTrigger.EnterActions>
                    <BeginStoryboard x:Name="Margin1BeginStoryboard">
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="FollowingMargin"
                                             To="{StaticResource MidFollowingMargin}" Duration="{StaticResource Margin1Duration}" />
                            <DoubleAnimation Storyboard.TargetProperty="PrecedingMargin"
                                             To="{StaticResource MidPrecedingMargin}" Duration="{StaticResource Margin1Duration}" />
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>
                <DataTrigger.ExitActions>
                    <RemoveStoryboard BeginStoryboardName="Margin1BeginStoryboard" />
                </DataTrigger.ExitActions>
            </DataTrigger>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=RelativeIndex, Converter={StaticResource PositionClipper}}" Value="2.0">
                <DataTrigger.EnterActions>
                    <BeginStoryboard x:Name="Margin2BeginStoryboard">
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="FollowingMargin"
                                             To="{StaticResource FollowingMarginBase}" Duration="{StaticResource Margin2Duration}" />
                            <DoubleAnimation Storyboard.TargetProperty="PrecedingMargin"
                                             To="{StaticResource PrecedingMarginBase}" Duration="{StaticResource Margin2Duration}" />
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>
                <DataTrigger.ExitActions>
                    <RemoveStoryboard BeginStoryboardName="Margin2BeginStoryboard" />
                </DataTrigger.ExitActions>
            </DataTrigger>
            
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="VerticalPathAlignment" Value="Top" />
                    <Condition Property="HorizontalPathAlignment" Value="Left" />
                </MultiTrigger.Conditions>
                <Setter Property="RenderTransformOrigin" Value="0.0,0.0" />
            </MultiTrigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="VerticalPathAlignment" Value="Top" />
                    <Condition Property="HorizontalPathAlignment" Value="Center" />
                </MultiTrigger.Conditions>
                <Setter Property="RenderTransformOrigin" Value="0.5,0.0" />
            </MultiTrigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="VerticalPathAlignment" Value="Top" />
                    <Condition Property="HorizontalPathAlignment" Value="Right" />
                </MultiTrigger.Conditions>
                <Setter Property="RenderTransformOrigin" Value="1.0,0.0" />
            </MultiTrigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="VerticalPathAlignment" Value="Center" />
                    <Condition Property="HorizontalPathAlignment" Value="Left" />
                </MultiTrigger.Conditions>
                <Setter Property="RenderTransformOrigin" Value="0.0,0.5" />
            </MultiTrigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="VerticalPathAlignment" Value="Center" />
                    <Condition Property="HorizontalPathAlignment" Value="Center" />
                </MultiTrigger.Conditions>
                <Setter Property="RenderTransformOrigin" Value="0.5,0.5" />
            </MultiTrigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="VerticalPathAlignment" Value="Center" />
                    <Condition Property="HorizontalPathAlignment" Value="Right" />
                </MultiTrigger.Conditions>
                <Setter Property="RenderTransformOrigin" Value="1.0,0.5" />
            </MultiTrigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="VerticalPathAlignment" Value="Bottom" />
                    <Condition Property="HorizontalPathAlignment" Value="Left" />
                </MultiTrigger.Conditions>
                <Setter Property="RenderTransformOrigin" Value="0.0,1.0" />
            </MultiTrigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="VerticalPathAlignment" Value="Bottom" />
                    <Condition Property="HorizontalPathAlignment" Value="Center" />
                </MultiTrigger.Conditions>
                <Setter Property="RenderTransformOrigin" Value="0.5,1.0" />
            </MultiTrigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="VerticalPathAlignment" Value="Bottom" />
                    <Condition Property="HorizontalPathAlignment" Value="Right" />
                </MultiTrigger.Conditions>
                <Setter Property="RenderTransformOrigin" Value="1.0,1.0" />
            </MultiTrigger>
        </Style.Triggers>
    </Style>
</ResourceDictionary>
